/*
 * 2005-11-19 lxm 创建。 
 */
package com.hd123.rumba.organization;

import java.util.ArrayList;
import java.util.List;

import com.hd123.rumba.domain.IDomain;
import com.hd123.rumba.exception.BusinessException;
import com.hd123.rumba.runtime.BaseEntity;
import com.hd123.rumba.runtime.NamingMgr;

/**
 * @author lxm
 * 
 */
public class Organization extends BaseEntity implements IOrganization {

  private String code;
  private String name;
  private IDomain organizationDomain;
  private IOrganization upperOrganization;
  private String remark;
  private String levelId;
  private List orgRoles = new ArrayList();

  /* orgRoles的复制列表 */
  private List orgRoles_clone;
  /* 使用中的组织角色列表 */
  private List usingOrgRoles;

  public String getCode() throws BusinessException {
    return code;
  }

  public void setCode(String code) throws BusinessException {
    if (code != null) {
      code = code.trim().toUpperCase();
      code = truncate(code, COrganization.LENGTH_CODE, "code", COrganization.CAPTION_CODE);
    }
    this.code = code;
  }

  public String getName() throws BusinessException {
    return name;
  }

  public void setName(String name) throws BusinessException {
    if (name != null)
      name = truncate(name, COrganization.LENGTH_NAME, "name", COrganization.CAPTION_NAME);
    this.name = name;
  }

  public IDomain getOrganizationDomain() throws BusinessException {
    return organizationDomain;
  }

  public void setOrganizationDomain(IDomain organizationDomain) throws BusinessException {
    // if (this.uuid != null)
    // throw new BusinessException("修改状态下，禁止修改" + CAPTION_ORGANIZATIONDOMAIN);
    this.organizationDomain = organizationDomain;
  }

  public String getRemark() throws BusinessException {
    return remark;
  }

  public void setRemark(String remarck) throws BusinessException {
    if (remarck != null)
      remarck = truncate(remarck, COrganization.LENGTH_REMARCK, "remarck",
          COrganization.CAPTION_REMARK);
    this.remark = remarck;
  }

  public IOrganization getUpperOrganization() throws BusinessException {
    return upperOrganization;
  }

  public void setUpperOrganization(IOrganization upperOrganization) throws BusinessException {
    this.upperOrganization = upperOrganization;
  }

  public String getLevelId() {
    return levelId;
  }

  public void setLevelId(String levelId) throws BusinessException {
    if (levelId != null)
      levelId = truncate(levelId, COrganization.LENGTH_LEVELID, "levelId",
          COrganization.CAPTION_LEVELID);
    this.levelId = levelId;
  }

  public List getOrgRoles() {
    return orgRoles;
  }

  public void setOrgRoles(List orgRoles) throws BusinessException {
    this.orgRoles = orgRoles;
    syncOrgRoles();
  }

  public List getOrgRoles(boolean withDeleted) throws BusinessException {
    if (orgRoles == null)
      return null;
    if (!orgRoles.equals(orgRoles_clone))
      syncOrgRoles();
    if (withDeleted)
      return orgRoles;
    else
      return usingOrgRoles;
  }

  public boolean hasRole(Class orgRoleMgrIntf) throws BusinessException {
    return hasRole(orgRoleMgrIntf, false);
  }

  public boolean hasRole(Class orgRoleMgrIntf, boolean withDeleted) throws BusinessException {
    IOrgRoleMgr roleMgr = (IOrgRoleMgr) NamingMgr.getInstance().lookupMgr(orgRoleMgrIntf);
    boolean ret = false;
    for (int i = 0; i < orgRoles.size(); i++) {
      IOrgRole role = (IOrgRole) orgRoles.get(i);
      if (!withDeleted && COrgRole.VALUE_STATE_DELETED.equals(role.getState()))
        continue;
      if (roleMgr.canManage(role)) {
        ret = true;
        break;
      }
    }
    return ret;
  }

  public IOrgRole getRole(Class orgRoleMgrIntf) throws BusinessException {
    IOrgRoleMgr roleMgr = (IOrgRoleMgr) NamingMgr.getInstance().lookupMgr(orgRoleMgrIntf);
    return getRole(roleMgr, false);
  }

  public IOrgRole getRole(Class orgRoleMgrIntf, boolean withDeleted) throws BusinessException {
    IOrgRoleMgr roleMgr = (IOrgRoleMgr) NamingMgr.getInstance().lookupMgr(orgRoleMgrIntf);
    return getRole(roleMgr, withDeleted);
  }

  public IOrgRole getRole(IOrgRoleMgr roleMgr) throws BusinessException {
    return getRole(roleMgr, false);
  }

  public IOrgRole getRole(IOrgRoleMgr roleMgr, boolean withDeleted) throws BusinessException {
    IOrgRole orgRole = null;
    for (int i = 0; i < orgRoles.size(); i++) {
      IOrgRole role = (IOrgRole) orgRoles.get(i);
      if (!withDeleted && COrgRole.VALUE_STATE_DELETED.equals(role.getState()))
        continue;
      if (roleMgr.canManage(role)) {
        orgRole = role;
        break;
      }
    }
    return orgRole;
  }

  public String getCodeName() throws BusinessException {
    return getCode() + "[" + getName() + "]";
  }

  public String getNameCode() throws BusinessException {
    return getName() + "[" + getCode() + "]";
  }

  public String debugInfo() throws BusinessException {
    return getCode();
  }

  public boolean hasInternalRole() throws BusinessException {
    for (int i = 0; i < orgRoles.size(); i++) {
      IOrgRole role = (IOrgRole) orgRoles.get(i);
      if (COrgRole.VALUE_STATE_DELETED.equals(role.getState()))
        continue;
      if (COrgRole.VALUE_TYPE_INTERNAL.equals(role.getType())) {
        return true;
      }
    }
    return false;
  }

  public boolean hasExternalRole() throws BusinessException {
    for (int i = 0; i < orgRoles.size(); i++) {
      IOrgRole role = (IOrgRole) orgRoles.get(i);
      if (COrgRole.VALUE_STATE_DELETED.equals(role.getState()))
        continue;
      if (COrgRole.VALUE_TYPE_EXTERNAL.equals(role.getType())) {
        return true;
      }
    }
    return false;
  }

  private void syncOrgRoles() throws BusinessException {
    orgRoles_clone = new ArrayList();
    usingOrgRoles = new ArrayList();
    for (int i = 0; i < orgRoles.size(); i++) {
      IOrgRole orgRole = (IOrgRole) orgRoles.get(i);
      orgRoles_clone.add(orgRole);
      if (!COrgRole.VALUE_STATE_DELETED.equals(orgRole.getState()))
        usingOrgRoles.add(orgRole);
    }
  }
}
